package com.servlet.form;

import java.io.IOException;
import java.util.ArrayList;
import java.util.TreeSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.listener.ContextListener;
import com.servlet.model.algorithmes.AccountComparator;
import com.servlet.model.base.Contact;
import com.servlet.model.base.Profile;
import com.servlet.model.database.BaseAcces;

public class ExplorerForm extends HttpServlet
{
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		boolean correct = false;
		
		HttpSession session = request.getSession();
		
		//BaseAcces base = (BaseAcces) this.config.getServletContext().getAttribute("base");
		
		String email = (String) session.getAttribute("email");
		
		BaseAcces base = ContextListener.base;
		
		String nom = request.getParameter("nom");
		String specialite = request.getParameter("specialite");
		String societe = request.getParameter("societe");
		String ecole = request.getParameter("ecole");
		
		String resultat = null;

		if( nom != null && specialite != null && societe != null && ecole != null )
		{
			correct = true;
		}

		if( correct )
		{
			TreeSet<String> resultats = new TreeSet<String>();
			TreeSet<String> resultats1 = null;
			TreeSet<String> resultats2 = null;
			TreeSet<String> resultats3 = null;
			TreeSet<String> resultats4 = null;
			
			boolean  fill = false;
			
			if( Format.verifyName( nom ) )
			{
				resultats1 = new TreeSet<String>();
				resultats1.addAll( base.findEmailByName(nom) );
				fill = true;
			}
			
			if( specialite.compareTo("Non spécifié") != 0 )
			{
				resultats2 = new TreeSet<String>();
				resultats2.addAll( base.findEmailBySpecialite(specialite) );
				fill = true;
			}
			
			if( Format.verifyName( societe ) )
			{
				resultats3 = new TreeSet<String>();
				resultats3.addAll( base.findEmailByEnterprise(societe));
				fill = true;
			}
			
			if( Format.verifyName( ecole ) )
			{
				resultats4 = new TreeSet<String>();
				resultats4.addAll( base.findEmailBySchool(ecole) );
				//System.out.println(resultats.size() );
				fill = true;
			}
			
			
			String[] aares = null;
			
			if( fill )
			{
				if( resultats1 != null )
					aares = resultats1.toArray(new String[0]);
				else if( resultats2 != null )
					aares = resultats2.toArray(new String[0]);
				else if( resultats3 != null )
					aares = resultats3.toArray(new String[0]);
				else if( resultats4 != null )
					aares = resultats4.toArray(new String[0]);
				
				for( int i = 0 ; i < aares.length ; i++ )
				{
					if( resultats1 == null || resultats1.contains(aares[i] ) )
					{
						if( resultats2 == null || resultats2.contains(aares[i] ) )
						{
							if( resultats3 == null || resultats3.contains(aares[i] ) )
							{
								if( resultats4 == null || resultats4.contains(aares[i] ) )
								{
									resultats.add(aares[i]);
								}
							}
						}
					}
				}

			}
				
			if( !fill )
			{
				resultats.addAll( base.findEveryEmail() );
			}
			
			
			//System.out.println(resultats);

			resultats.remove(email);
			
			ArrayList<Contact> contacts = base.findContacts(email);

			String[] ares = resultats.toArray(new String[0]);
			double[] valProx = new double[ares.length];
			
			AccountComparator<Profile> ac = new AccountComparator<Profile>();
			
			for( int i = 0 ; i < ares.length ; i++ )
			{
				//ordre - si l'inverse
				valProx[i] = -ac.accountComparaison(email, ares[i] );
			}
			
			TreeSet<Profile> resultatsFinaux = new TreeSet<Profile>( ac );
			
			for( int i = 0 ; i < ares.length ; i++ )
			{
				Profile p = base.findProfile(ares[i]);
				p.setProx( valProx[i] );
				p.setMode( 0 );
				
				for( int j = 0 ; j < contacts.size() ; j++ )
				{
					if( p.getEmail().compareTo( contacts.get(j).getEmail_user1() ) == 0 || p.getEmail().compareTo( contacts.get(j).getEmail_user2() ) == 0 )
					{
						if(  !contacts.get(j).getIs_accepted() )
						{
							p.setMode( 1 );
						}
						else
						{
							p.setMode( 2 );
						}
					}
				}
				
				if( p.getMode() != 2)
					resultatsFinaux.add(p);
			}
			
			//System.out.println("Res : " + resultatsFinaux);
			session.setAttribute("recherche",  resultatsFinaux );
			session.setAttribute("rechercheAppuie",  resultatsFinaux );
		}
		else
		{
			
		}

		response.sendRedirect("explorer");
	}
}